package com.kalvin.kvf.modules.dy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kalvin.kvf.common.controller.BaseController;
import com.kalvin.kvf.common.dto.R;
import com.kalvin.kvf.common.utils.ShiroKit;
import com.kalvin.kvf.modules.dy.entity.Task;
import com.kalvin.kvf.modules.dy.service.TaskService;
import com.kalvin.kvf.modules.hxy.entity.Account;
import com.kalvin.kvf.modules.hxy.mapper.AccountMapper;
import com.kalvin.kvf.modules.sys.entity.User;
import com.kalvin.kvf.modules.sys.mapper.UserMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 * @since 2023-10-28 16:55:28
 */
@RestController
@RequestMapping("dy/taskone")
public class TaskOneController extends BaseController {

    @Autowired
    private TaskService taskService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AccountMapper accountMapper;

    @RequiresPermissions("dy:taskone:index")
    @GetMapping("index")
    public ModelAndView index() {
        return new ModelAndView("dy/task_one");
    }

    @GetMapping(value = "edit")
    public ModelAndView edit(Long id) {
        ModelAndView mv = new ModelAndView("dy/task_one_edit");
        Task task;
        if (id == null) {
            task = new Task();
        } else {
            task = taskService.getById(id);
        }
        mv.addObject("editInfo", task);
        return mv;
    }

    @GetMapping(value = "list/data")
    public R listData(Task task) {
        Page<Task> page = taskService.listTaskPage(task);
        return R.ok(page);
    }

    @RequiresPermissions("dy:taskone:add")
    @PostMapping(value = "add")
    public R add(Task task) throws Exception {
        User user = userMapper.selectById(ShiroKit.getUserId());
        Account account = accountMapper.selectOne(new QueryWrapper<Account>().eq("phone", user.getUsername()));
        if (account==null){
            throw new Exception("未配置账户信息 请联系负责人！");
        }
        if (task.getCustNum()>account.getCustNum()){
            throw new Exception("点数不足,请联系负责人充值！");
        }
        String shieldKeywords = task.getShieldKeywords().replace("，", ",");
        String siftKeywords = task.getSiftKeywords().replace("，", ",");
        task.setAccountId(account.getId());
        task.setPhone(ShiroKit.getUser().getUsername());
        task.setShieldKeywords(shieldKeywords);
        task.setSiftKeywords(siftKeywords);
        taskService.save(task);
        return R.ok();
    }

    @RequiresPermissions("dy:taskone:del")
    @PostMapping(value = "batchdel")
    public R batchdel(@RequestParam("ids") List<Long> ids) {
        taskService.removeByIds(ids);
        return R.ok();
    }

    @RequiresPermissions("dy:taskone:edit")
    @PostMapping(value = "edit")
    public R edit(Task task) {
        taskService.updateById(task);
        return R.ok();
    }

    @RequiresPermissions("dy:taskone:del")
    @PostMapping(value = "del/{id}")
    public R del(@PathVariable Long id) {
        taskService.removeById(id);
        return R.ok();
    }

    @GetMapping(value = "get/{id}")
    public R get(@PathVariable Long id) {
        return R.ok(taskService.getById(id));
    }


}

